package net.sunofbeaches.services.impl;

import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import net.sunofbeaches.dao.FriendLinkDao;
import net.sunofbeaches.pojo.Category;
import net.sunofbeaches.pojo.FriendLink;
import net.sunofbeaches.pojo.SobUser;
import net.sunofbeaches.response.ResponseResult;
import net.sunofbeaches.services.FriendLinkService;
import net.sunofbeaches.services.IUserService;
import net.sunofbeaches.utils.Constants;
import net.sunofbeaches.utils.IdWorker;
import net.sunofbeaches.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class FriendLinkServiceImpl implements FriendLinkService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private FriendLinkDao friendLinkDao;

    /**
     * 添加友情连接
     *
     * @param friendLink
     * @return
     */
    @Override
    public ResponseResult addFriendLink(FriendLink friendLink) {
        //判断数据
        String url = friendLink.getUrl();
        if (TextUtils.isEmpty(url)) {
            return ResponseResult.FAILED("链接Url不可以为空");
        }
        String logo = friendLink.getLogo();
        if (TextUtils.isEmpty(logo)) {
            return ResponseResult.FAILED("logo不可以为空.");
        }
        String name = friendLink.getName();
        if (TextUtils.isEmpty(name)) {
            return ResponseResult.FAILED("对方网站名不可以为空.");
        }
        //补全数据
        friendLink.setId(idWorker.nextId() + "");
        friendLink.setUpdateTime(new Date());
        friendLink.setCreateTime(new Date());
        friendLink.setState("1");
        //保存数据
        friendLinkDao.save(friendLink);
        return ResponseResult.SUCCESS("添加成功.");
    }

    @Override
    public ResponseResult deleteFriendLink(String friendLinkId) {
        int result = friendLinkDao.deleteAllById(friendLinkId);
        if (result == 0) {
            return ResponseResult.FAILED("删除失败");
        }
        return ResponseResult.SUCCESS("删除成功");
    }

    /**
     * 更新内容有什么：
     * logo
     * 对方网站的名称
     * url
     * order
     *
     * @param friendLinkId
     * @param friendLink
     * @return
     */
    @Override
    public ResponseResult updateFriendLink(String friendLinkId, FriendLink friendLink) {
        FriendLink friendLinkFromDb = friendLinkDao.findOneById(friendLinkId);
        if (friendLinkFromDb == null) {
            return ResponseResult.FAILED();
        }
        String logo = friendLink.getLogo();
        if (!TextUtils.isEmpty(logo)) {
            friendLinkFromDb.setLogo(logo);
        }
        String name = friendLink.getName();
        if (!TextUtils.isEmpty(name)) {
            friendLinkFromDb.setName(name);
        }
        String url = friendLink.getUrl();
        if (!TextUtils.isEmpty(url)) {
            friendLinkFromDb.setUrl(url);
        }
        friendLinkFromDb.setOrder(friendLink.getOrder());
        friendLinkFromDb.setUpdateTime(new Date());
        friendLinkFromDb.setState("1");
        friendLinkDao.save(friendLinkFromDb);
        return ResponseResult.SUCCESS("更新成功");
    }
    @Autowired
    private IUserService iUserService;

    @Override
    public ResponseResult listFriendLinks() {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");
        SobUser sobUser = iUserService.checkSobUser();
        List<FriendLink> all;
        if (sobUser == null || !Constants.User.ROLE_ADMIN.equals(sobUser.getRoles())) {
            all = friendLinkDao.listFriendLinkByState("1");
        } else {
            all = friendLinkDao.findAll(sort);
        }
        return ResponseResult.SUCCESS("获取列表成功.").setData(all);
    }

    @Override
    public ResponseResult getFriendLink(String friendLinkId) {
        FriendLink friendLink = friendLinkDao.findOneById(friendLinkId);
        if (friendLink == null) {
            return ResponseResult.SUCCESS("友情链接不存在");
        }
        return ResponseResult.SUCCESS("获取成功").setData(friendLink);
    }
}
